<template>
  <DaprPlusTable
    :table-title="$t('fsb.games.memoryAddress.list')"
    :entity="entity"
    :grid-options="gridOptions"
    :query-api="crudApi.getPage"
    :create-api="crudApi.create"
    :update-api="crudApi.update"
    :delete-api="crudApi.delete"
    :default-new-record="createDefaultMemoryAddress"
    @update:grid-api="onGridApiUpdate"
  />
</template>

<script setup lang="ts">
import type { ExtendedVxeGridApi, VxeGridProps } from '#/adapter/vxe-table';
import { DaprPlusTable } from '#/components/daprplus';
import { createCrudApi } from '#/api/daprplus';
import type { IOutDto, IInDto, EntityInfo } from '#/utils/daprplus';
import { createBooleanColumn } from '#/utils/daprplus';
import { $t } from '#/locales';

const baseUrl = '/games/api/memoryaddress';
const crudApi = createCrudApi<MemoryAddressInDto, MemoryAddressOutDto>(baseUrl);

const entity: EntityInfo = {
  code: 'MemoryAddress',
  display: $t('fsb.games.memoryAddress.title'),
};

// 验证规则
const validRules = {
  name: [
    { required: true, message: '地址名称不能为空' },
    { min: 2, max: 50, message: '地址名称长度应在2-50个字符之间' },
  ],
  dataType: [{ required: true, message: '数据类型不能为空' }],
  offset: [{ required: true, message: '偏移量不能为空' }],
  findOffset: [{ required: true, message: '查找偏移不能为空' }],
  addrRange: [{ max: 200, message: '地址范围长度应在0-200个字符之间' }],
  featureCode: [{ max: 500, message: '特征码长度应在0-500个字符之间' }],
  featureSource: [{ max: 500, message: '特征源长度应在0-500个字符之间' }],
  relateOffsets: [{ max: 500, message: '关联偏移长度应在0-500个字符之间' }],
};

// 扩展配置
const gridOptions: VxeGridProps<MemoryAddressOutDto> = {
  editRules: validRules,
  columns: [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '编号',
      sortable: true,
      width: 130,
    },
    {
      field: 'name',
      title: '地址名称',
      sortable: true,
      align: 'left',
      width: 150,
      editRender: { name: 'input' },
    },
    {
      field: 'dataType',
      title: '类型',
      sortable: true,
      width: 100,
      editRender: { name: 'input' },
    },
    {
      field: 'offset',
      title: '偏移量',
      sortable: true,
      width: 120,
      editRender: { name: 'input' },
    },
    {
      field: 'baseAddressId',
      title: '上级地址',
      width: 130,
      editRender: { name: 'input' },
    },
    createBooleanColumn('isFixed', '修复', { width: 90 }),
    {
      field: 'addrRange',
      title: '查找范围',
      width: 120,
      editRender: { name: 'input' },
    },
    {
      field: 'findOffset',
      title: '查找偏移',
      width: 125,
      editRender: { name: 'input', props: { type: 'number' } },
    },
    {
      field: 'featureCode',
      title: '特征码',
      width: 150,
      editRender: { name: 'input' },
    },
    {
      field: 'featureSource',
      title: '特征源',
      width: 150,
      editRender: { name: 'input' },
    },
    {
      field: 'relateOffsets',
      title: '关联偏移',
      width: 150,
      editRender: { name: 'input' },
    },
    createBooleanColumn('isReadData', '读取', { width: 90 }),
    createBooleanColumn('isCall', 'Call', { width: 90 }),
    createBooleanColumn('isLocked', '锁定', { width: 90 }),
    {
      field: 'createdTime',
      title: '创建时间',
      formatter: 'formatDateTime',
      width: 130,
    },
  ],
};

let gridApi: ExtendedVxeGridApi;

function onGridApiUpdate(api: ExtendedVxeGridApi) {
  gridApi = api;
}

function createDefaultMemoryAddress(): MemoryAddressInDto {
  return {
    id: '0',
    name: '新内存地址',
    dataType: '',
    offset: '',
    findOffset: 0,
    isReadData: false,
    isCall: false,
    isFixed: false,
    isLocked: false,
  };
}

export interface MemoryAddressOutDto extends IOutDto {
  name?: string;
  dataType?: string;
  offset?: string;
  baseAddressId?: string;
  isFixed?: boolean;
  addrRange?: string;
  findOffset?: number;
  featureCode?: string;
  featureSource?: string;
  relateOffsets?: string;
  isReadData?: boolean;
  isCall?: boolean;
  isLocked?: boolean;
  createdTime?: Date;
}

export interface MemoryAddressInDto extends IInDto {
  name: string;
  dataType: string;
  offset: string;
  baseAddressId?: string;
  isFixed: boolean;
  addrRange?: string;
  findOffset: number;
  featureCode?: string;
  featureSource?: string;
  relateOffsets?: string;
  isReadData: boolean;
  isCall: boolean;
  isLocked: boolean;
}
</script>

<style scoped></style>
